package filter;

import java.awt.image.BufferedImage;
import projectcvip.*;
import utils.BasicOperations;

/**
 *
 * @author Michał
 */
public class FilterSupervisor extends JSBasic
{
    /**
     * Rozmiar maski.
     */
    private int maskSize;
    /**
     * Liczba kanalow, ktore zostana odczytane. Pole zostalo dodane, aby moc wybrac
     * pomiedzy trzema kanalami (RGB) a czterema (alfa + RGB).
     */
    private int noChannels;
    /**
     * Sciezka do obrazka poddawanego filtracji
     */
    private String inputPath;
    /**
     * Nazwa formatu obrazka. Np. png, jpeg, gif
     */
    private String formatName;
    
    public FilterSupervisor(int maskSize, int noChannels, String inputPath, String formatName)
    {
        this.maskSize = maskSize;
        this.noChannels = noChannels;
        this.inputPath = inputPath;
        this.formatName = formatName;
    }
    
    /*public String getPathForChannel(int noChannel)
    {
        return inputPath.substring(0, inputPath.indexOf('.')) + "_channel" + noChannel + ".dat";
    }*/
    
    public void printTask(FilterTask task)
    {
        System.out.println("Nadzorca zleca:");
        System.out.println("MaskSize: " + task.maskSize);
        System.out.println("ChannelShift: " + task.channelShift);
        System.out.println("Path: " + task.path);
        System.out.println("FormatName: " + task.formatName);
    }
    
    public void go()
    {
        try
        {
            System.out.println("Nadzorca zaczyna prace.");
            
            FilterTask task = new FilterTask();
            task.maskSize = maskSize;
            task.path = inputPath;
            task.formatName = formatName;
            for (int i = 0; i < noChannels; i++)
            {
                task.channelShift = i * 8;
                printTask(task);
                space.write(task, null, defaultLease);
            }

            FilterResult result = new FilterResult();
            int[][] channels = new int[noChannels][];
            for (int i = 0; i < noChannels; i++)
            {
                System.out.println("Nadzorca pobiera rozwiazanie...");
                result.filePath = null;
                result.channelShift = null;
                result = (FilterResult) space.take(result, null, defaultLease);

                //wczytaj kanal zapisany przez wykonawce
                channels[i] = BasicOperations.loadChannel(result.filePath, result.channelShift);
            }

            task.maskSize = null;
            task.channelShift = null;
            task.path = null;
            task.formatName = null;
            space.write(task, null, defaultLease); //trujaca pigulka

            String outputPath = inputPath.substring(0, inputPath.indexOf('.')) + "_filtered" + inputPath.substring(inputPath.indexOf('.'));

            BufferedImage src = BasicOperations.readImage(inputPath);
            BufferedImage resultImg = BasicOperations.mergeFilteredChannels(src, channels);
            BasicOperations.writeImage(resultImg, outputPath, formatName);
            
            System.out.println("Nadzorca konczy prace.");
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public void run()
    {
        long startTime = System.currentTimeMillis();
        go();
        long endTime = System.currentTimeMillis();
        System.out.println("Czas pracy nadzorcy: " + (endTime - startTime) + " ms.");
    }
}
